package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.ResumeEducationService;
import cn.iocoder.yudao.hires.vo.ResumeEducationPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeEducationSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.ResumeEducationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.ResumeEducationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 简历-教育经历 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResumeEducationServiceImpl implements ResumeEducationService {

    @Resource
    private ResumeEducationMapper educationMapper;

    @Override
    public Integer createEducation(ResumeEducationSaveReqVO createReqVO) {
        // 插入
        ResumeEducationDO education = BeanUtils.toBean(createReqVO, ResumeEducationDO.class);
        educationMapper.insert(education);
        // 返回
        return education.getId();
    }

    @Override
    public void updateEducation(ResumeEducationSaveReqVO updateReqVO) {
        // 校验存在
        validateEducationExists(updateReqVO.getId());
        // 更新
        ResumeEducationDO updateObj = BeanUtils.toBean(updateReqVO, ResumeEducationDO.class);
        educationMapper.updateById(updateObj);
    }

    @Override
    public void deleteEducation(Integer id) {
        // 校验存在
        validateEducationExists(id);
        // 删除
        educationMapper.deleteById(id);
    }

    private void validateEducationExists(Integer id) {
        if (educationMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "简历-教育经历不存在"));
        }
    }

    @Override
    public ResumeEducationDO getEducation(Integer id) {
        return educationMapper.selectById(id);
    }

    @Override
    public PageResult<ResumeEducationDO> getEducationPage(ResumeEducationPageReqVO pageReqVO) {
        return educationMapper.selectPage(pageReqVO);
    }

}